package cn.lysj.framework.config.controller;

import cn.lysj.framework.base.BaseController;
import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.config.domain.Config;
import cn.lysj.framework.config.service.ConfigService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author zwj
 * @description 价格配置表控制层
 * @create 2018-12-18 11:03:37
 **/
@Slf4j
@RestController
@Api(description = "价格配置表管理接口")
@RequestMapping("/config")
public class ConfigController extends BaseController<Config> {

    @Resource
    private ConfigService configService;

    @Override
    public ConfigService getService() {
        return configService;
    }


    @GetMapping("/list_rewrite")
    public Resp list(@CurrentUser User user, Config config, PageVo pageVo) {
        config.setStoreId(user.getStoreId());
        return new Resp(configService.list(config, pageVo));
    }

    @Override
    public Resp update(Config entity) {
        if (null == entity.getBoMoney()) {
            return new Resp(Resp.PARAM_ERROR, "餐盒费不能为空");
        }
        if (null == entity.getCutleryMoney()) {
            return new Resp(Resp.PARAM_ERROR, "餐具费不能为空");
        }
        if (entity.getBoMoney().compareTo(BigDecimal.ZERO) == -1) {
            return new Resp(Resp.PARAM_ERROR, "餐盒费不能为负数");
        }
        if (entity.getCutleryMoney().compareTo(BigDecimal.ZERO) == -1) {
            return new Resp(Resp.PARAM_ERROR, "餐具费不能为负数");
        }
        Config config = configService.findOne(entity.getId());
        config.setBoMoney(entity.getBoMoney());
        config.setCutleryMoney(entity.getCutleryMoney());
        configService.save(config);
        return new Resp("修改成功");
    }
}
